package com.neuedu.wc;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class WordCount2 {
    public static void main(String[] args) {
        try {
            if (args == null || args.length != 2) {
                System.out.println("useages: hadoop jar xxx.jar WordCount /books /wc2");
                return;
            }
            Configuration conf = new Configuration();
            FileSystem hdfs = FileSystem.get(conf);
            //定义输入目录
            // String input="/books";
            String input = args[0];
            //String output= "/wordcount";
            String output = args[1];
            Path outputPath = new Path(output);
            // 判断输出目录是否存在，存在则删除之
            if (hdfs.exists(outputPath)) {
                hdfs.delete(outputPath, true);
            }
            // 创建Job任务
            Job job = Job.getInstance(conf, "word count");
            job.setJarByClass(WordCount2.class);
            //设置输入
            job.setInputFormatClass(TextInputFormat.class);
            FileInputFormat.setInputPaths(job, input);
            // 设置mapper
            job.setMapperClass(WordCountMapper.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            // 设置reducer
            job.setReducerClass(WordCountReducer.class);
            // 设置输出:下面2行容易写错
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
            job.setOutputFormatClass(TextOutputFormat.class);
            FileOutputFormat.setOutputPath(job, outputPath);
            // 运行
            Boolean flag = job.waitForCompletion(true);
            if (flag) {
                System.out.println("词频统计结束~~~");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String line = value.toString();
            if (StringUtils.isBlank(line)) {
                return;
            }
            String[] items = line.split(" ");
            for (String word : items) {
                context.write(new Text(word), new IntWritable(1));
            }
        }
    }

    public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            context.write(key, new IntWritable(sum));
        }

    }
}
